package com.hyh.admin.sys.controller.base;


import com.hyh.ad.common.annotation.Log;
import com.hyh.ad.common.core.controller.BaseController;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.core.domain.model.SysMenu;
import com.hyh.ad.common.core.page.TableDataInfo;
import com.hyh.ad.common.enums.BusinessType;
import com.hyh.admin.sys.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sysMenu")
@Api(tags = "系统菜单管理")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 查询所有菜单
     * @param sysMenu
     * @return 分页数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有菜单")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    public TableDataInfo list(SysMenu sysMenu){
        startPage();
        List<SysMenu> sysMenus = sysMenuService.selectListSysMenuPage(sysMenu);
        return getDataTable(sysMenus);
    }

    /**
     * 查询菜单详情
     * @param   id
     * @return  单个菜单数据
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询菜单详情")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(sysMenuService.selectSysMenuById(id));
    }

    /**
     * 新增菜单
     * @param   sysMenu
     * @return  结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    @Log(businessName = BusinessType.INSERT, operationModule = "新增菜单")
    public AjaxResult add(@RequestBody SysMenu sysMenu) {
        return toAjax(sysMenuService.insertSysMenu(sysMenu));
    }

    /**
     * 修改菜单
     * @param   sysMenu
     * @return  结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    @Log(businessName = BusinessType.UPDATE, operationModule = "修改菜单")
    public AjaxResult edit(@RequestBody SysMenu sysMenu) {
        return toAjax(sysMenuService.updateSysMenu(sysMenu));
    }

    /**
     * 删除菜单
     * @param   ids
     * @return  结果
     */
    @DeleteMapping("/remove/{ids}")
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    @Log(businessName = BusinessType.DELETE, operationModule = "删除菜单")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysMenuService.deleteSysMenuByIds(ids));
    }

    /**
     * 根据父亲id查询菜单
     * @return  菜单列表
     */
    @GetMapping("/treeselect/{parentId}")
    @ApiOperation(value = "获取菜单下拉树列表")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    public AjaxResult treeselect(@PathVariable("parentId") Long parentId) {
        return AjaxResult.success(sysMenuService.selectSysMenuTree(parentId));
    }

    /**
     * 根据菜单名字查询菜单
     * @return  菜单列表
     */
    @GetMapping("/selectByMenuName")
    @ApiOperation(value = "根据菜单名字查询菜单")
    public AjaxResult selectByMenuName(@RequestParam String menuName) {
        return AjaxResult.success(sysMenuService.selectByMenuName(menuName));
    }

    /**
     * 更新菜单状态
     * @param
     * @return
     */
    @PutMapping("/changeStatus")
    @ApiOperation(value = "更新菜单状态")
    @PreAuthorize("hasAnyAuthority('sys:admin:permission','sys:crud:user')")
    @Log(businessName = BusinessType.UPDATE, operationModule = "更新菜单状态")
    public AjaxResult changeStatus(@RequestBody SysMenu sysMenu) {
        return toAjax(sysMenuService.updateSysMenu(sysMenu));
    }
}
